package com.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.OrderBean;
import com.project.bean.ShopCarBean;
import com.project.mapper.IOrderInfoMapper;
import com.project.mapper.IOrderMapper;
import com.project.mapper.IShopCarMapper;
import com.project.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private IOrderInfoMapper infoMapper;
    @Autowired
    private IShopCarMapper shopCarMapper;

    @Override
    public void add(OrderBean order, int userId) {
        //添加订单
        orderMapper.addOrder(order,userId);
        System.out.println("1111111111111111111111111111111111111111111");
        //按用户id得到购物车集合
        List<ShopCarBean>shopList = shopCarMapper.findByUser(userId);
        System.out.println("22222222222222222222222222222222222222222222");
        System.out.println(shopList);
        //添加订单明细
        infoMapper.addInfo(order.getId(),shopList);
        System.out.println("33333333333333333333333333333333333333333333");
        //清空用户购物车
        shopCarMapper.delByUser(userId);
    }

    @Override
    public PageInfo<OrderBean> findByItem(int pageNo, String status, LocalDate startDate, LocalDate endDate, Integer userId) {
        PageHelper.startPage(pageNo,3);
        PageInfo<OrderBean>pageInfo=PageInfo.of(orderMapper.findByItem(status,startDate,endDate,userId));
        return pageInfo;
    }

    @Override
    public OrderBean findById(int id) {
        return orderMapper.findById(id);
    }

    @Override
    public void send(int id) {
        orderMapper.send(id);
    }

    @Override
    public List<OrderBean> findByItemStatus() {
        return orderMapper.findByItemStatus();
    }

    @Override
    public void finish(int id) {
        orderMapper.finish(id);
    }

}
